package main.java.com.bookmanager.service;

import main.java.com.bookmanager.model.Book;
import main.java.com.bookmanager.util.Classify;
import main.java.com.bookmanager.util.MemoryStorage;

import java.util.Date;
import java.util.List;

public class BookService {
    MemoryStorage storage = new MemoryStorage();

    // 添加
    public String addBook(Classify classify, int bookId, String bookName, String bookAuthor, String status , int quantity) {
        Date createTime = new Date();
        Book book = new Book(bookId, bookName, bookAuthor, classify, status, createTime , quantity);
        if (storage.addBooks(classify, book)) {
            return "添加成功";
        } else {
            return "添加失败";
        }
    }

    // 删除
    public String deleteBook(Classify classify, String query) {
        if (isNumeric(query)) {
            int bookId = Integer.parseInt(query);
            if (storage.deleteBooksId(classify, bookId)) {
                return "删除成功";
            }
        } else {
            if (storage.deleteBooksName(classify, query)) {
                return "删除成功";
            }
        }
        return "删除失败";
    }

    // 查找(ID)
    public Book getBooks(Classify classify, int id) {
        return storage.getBooksId(classify, id);
    }

    // 修改
    public String amendBook(Classify classify, String oldBookName, int bookId, String bookName, String bookAuthor, Classify classify1, String status, int quantity) {
        Date createTime = new Date();
        if (storage.amendBooksName(classify, oldBookName, bookId, bookName, bookAuthor, status, classify1, createTime, quantity)) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }

    public List<Book> getAllBooks(Classify classify) {
        List<Book> books = storage.getAllBooks(classify);
        return books;
    }

    private boolean isNumeric(String str) {
        try {
            Integer.parseInt(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
}